import 'package:flutter/material.dart';
import 'package:flutter_cate/core/model/meal_model.dart';
import 'package:flutter_cate/core/viewmodel/favor_view_model.dart';
import 'package:flutter_cate/core/extension/int_extension.dart';
import 'package:flutter_cate/ui/pages/detail/detail.dart';
import 'package:provider/provider.dart';
import 'operation_item.dart';

final cardRadius = 12.px;

class AMMealItem extends StatelessWidget {

  final AMMealModel _meal;
  AMMealItem(this._meal);

  @override
  Widget build(BuildContext context) {
    return Card(
      margin: EdgeInsets.all(10.px),
      elevation: 5,
      shape: RoundedRectangleBorder(borderRadius: BorderRadius.circular(cardRadius)),
      child: Column(
        children: <Widget>[
          buildBasicInfo(context),
          buildOperationInfo()
        ],
      ),
    );
  }

  Widget buildBasicInfo (BuildContext context) {
    return GestureDetector(
      onTap: (){
        Navigator.of(context).pushNamed(AMDetailScreen.routeName, arguments: _meal);
      },
      child: Stack(
        children: <Widget>[
          ClipRRect(
            borderRadius: BorderRadius.only(
              topLeft: Radius.circular(cardRadius),
              topRight: Radius.circular(cardRadius)
            ),
            child: Image.network(_meal.imageUrl, width: double.infinity, height: 250.px, fit: BoxFit.cover,),
          ),
          Positioned(
              right: 10.px,
              bottom: 10.px,
              left: 10.px,
              child: Container(
                width: 300.px,
                padding: EdgeInsets.symmetric(horizontal: 10.px,vertical: 5.px),
                decoration: BoxDecoration(
                  color: Colors.black54,
                  borderRadius: BorderRadius.circular(6.px)
                ),
                child: Text(_meal.title, style: Theme.of(context).textTheme.headline3.copyWith(color: Colors.white),textAlign: TextAlign.center,),
              ))
        ],
      ),
    );
  }

  Widget buildOperationInfo() {
    return Padding(
      padding: EdgeInsets.all(5.px),
      child: Row(
        mainAxisAlignment: MainAxisAlignment.spaceAround,
        children: [
          AMOperationItem(Icon(Icons.schedule), "${_meal.duration}min"),
          AMOperationItem(Icon(Icons.restaurant), "${_meal.complexStr}"),
          buildFavorItem()
        ],
      ),
    );
  }
  
  Widget buildFavorItem() {
    return Consumer<AMFavorViewModel>(
      builder: (ctx, favorVM, child) {
        // 1.判断收藏状态
        final iconData = favorVM.isFavor(_meal) ? Icons.favorite: Icons.favorite_border;
        final favorColor = favorVM.isFavor(_meal) ? Colors.red : Colors.black;
        final title = favorVM.isFavor(_meal) ? "收藏" : "未收藏";
        return GestureDetector(
          child: AMOperationItem(
            Icon(iconData,color: favorColor,),
            title,
            textColor: favorColor,
          ),
          onTap: () {
            favorVM.handleMeal(_meal);
          },
        );
      },
    );
  }
}
